﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Data;
using KPIS.Utils;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.APP.Classes;


namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PR_BonusCommand : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private object _dataItem = null;
        private Command_BLL salaryCommandBLL = new Command_BLL();
        private IMasRecordStatusBLL staComBLL = new MasRecordStatusBLL();
        private IEmployeeTypeBLL empTypeBLL = new EmployeeTypeBLL();
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private IJobSiteLevelSystemControlBLL jslscBLL = new JobSiteLevelSystemControlBLL();
        Main main = new Main();
        private IJobSiteTypeBLL jobTypeBLL = new JobSiteTypeBLL();

        //ReadOnly property.
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        //Initializing the new variable updating ViewState data.
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {
                bindStaCom();
                bindEmpType();
                
                DataSet ds = new DataSet();
                ds = jslscBLL.ListJobSiteLevelSystemControl();
                bool bMis = ds.Tables[0].Rows[0][0].ToString() == "1";
                //if (ds.Tables[0].Rows[0][0].ToString() == "3") //กระทรวง
                //{
                    rdoFlag.Items[0].Enabled = !bMis;
                    rdoFlag.Items[1].Selected = bMis;
                    rdoApptypeOut.Enabled = !bMis;
                    txtAppName.Enabled = !bMis;
                //}
                //else 
                //{
                //    rdoFlag.Items[2].Enabled = false;
                    rdoFlag.Items[0].Selected = !bMis;
                //    rdoApptypeOut.Enabled = true;
                //    txtAppName.Enabled = true;
                //}
                bindEmp();


                
                this.ViewState.Add("IsUserControlPostBack", true);
            }

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblID.Text = DataBinder.Eval(DataItem, "cmd_seq").ToString();
                    ddlStatus.SelectedValue = DataBinder.Eval(DataItem, "cmd_status").ToString();
                    string strFlag = DataBinder.Eval(DataItem, "cmd_flag").ToString();
                    if(strFlag == "1")
                        rdoFlag.Items[0].Selected = true;
                    if(strFlag == "2")
                        rdoFlag.Items[1].Selected = true;
                    if (strFlag == "3")
                        rdoFlag.Items[2].Selected = true;

                    if (ddlStatus.SelectedValue == "W")
                        txtNo.Text = DataBinder.Eval(DataItem, "cmd_draft_no").ToString();
                    else
                        txtNo.Text = DataBinder.Eval(DataItem, "cmd_ref_no").ToString();

                    txtName.Text = DataBinder.Eval(DataItem, "cmd_name").ToString();

                    ddlEmployeeType.SelectedValue = DataBinder.Eval(DataItem, "emp_type_seq").ToString();

                    string AppID = DataBinder.Eval(DataItem, "approved_seq").ToString();
                    if (AppID != "")
                    {
                        cmbEmployee.SelectedValue = AppID;
                        rdoApptypeIn.Checked = true;
                        rdoApptypeOut.Checked = false;
                        txtAppName.Enabled = false;
                    }
                    else
                    {
                        txtAppName.Text = DataBinder.Eval(DataItem, "approved_name").ToString() != "" ? DataBinder.Eval(DataItem, "approved_name").ToString() : "-";
                        rdoApptypeIn.Checked = false;
                        rdoApptypeOut.Checked = true;
                        txtAppName.Enabled = true;

                        cmbEmployee.Enabled = false;
                        cmbEmployee.ClearSelection();
                        cmbEmployee.Text = string.Empty;
                        cmbEmployee.Text = "... เลือกบุคลากร ...";
                    }

                    try
                    {
                        string mydate = DataBinder.Eval(DataItem, "approved_date").ToString();  // --------  4/15/2009 12:00:00 AM
                        string[] str = mydate.Split('/');
                        str[2] = (int.Parse(str[2].Substring(0, 4)) + 543).ToString();
                        mydate = str[1] + "/" + str[0] + "/" + str[2];
                        txtDate.SetValue(mydate);
                    }
                    catch
                    { }


                    txtAppPosition.Text = DataBinder.Eval(DataItem, "approved_positions").ToString();

                    //txtRemarks.Text = DataBinder.Eval(DataItem, "remarks").ToString();





                    this.btnAddnew.Visible = false;
                    this.btnUpdate.Visible = true;
                }
                else
                {
                    this.btnUpdate.Visible = false;
                    this.btnAddnew.Visible = true;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void bindStaCom()
        {
            IList<MasRecordStatusInfo> ListStatusCommand = staComBLL.ListStatusCommand();
            ddlStatus.DataSource = ListStatusCommand;
            ddlStatus.DataBind();
        }
        private void bindEmpType()
        {
            IList<EmployeeTypeInfo> infoList = empTypeBLL.ListEmployeeTypes();
            ddlEmployeeType.DataSource = infoList;
            ddlEmployeeType.DataBind();
        }

        protected void bindEmp()
        {
            JobSiteTypeInfo LisJobSiteType;
            IList<JobSiteTypeInfo> infoJobSiteType = jobTypeBLL.ListJobSiteTypes(int.Parse(rdoFlag.SelectedValue));
            LisJobSiteType = infoJobSiteType[0];

            IList<EmployeeInfo> infoList = empBLL.ListEmployeeByTypes("1", LisJobSiteType.Sequence.ToString(), string.Empty);

            foreach (EmployeeInfo List in infoList)
            {
                RadComboBoxItem item = new RadComboBoxItem();

                item.Text = List.Person.NamePrefix.Name + List.Person.FirstName + " " + List.Person.LastName;
                item.Value = List.Sequence.ToString();

                item.Attributes.Add("EMP_NAME", List.Person.NamePrefix.Name + List.Person.FirstName + " " + List.Person.LastName);
                item.Attributes.Add("EMP_NAME_ENG", List.Person.FirstNameEng + " " + List.Person.LastNameEng);
                //item.Attributes.Add("POS_NAME", List.Position == null ? "" : List.Position.Name);
                //item.Attributes.Add("CLASS_NAME", List.Class.Name);

                cmbEmployee.Items.Add(item);
                item.DataBind();
            }
        }
 
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            try
            {
                string cmdDraftNo="",cmdRefNo="";
                string mydate = txtDate.GetStringValue();  //18/03/2553
                if (mydate == "")
                {
                    main.MessageAlert(this.Page, "กรุณาระบุวันที่ลง!!");
                    return;
                }
                mydate = Utils.Date.ToEngFormatDate(mydate);


                if (ddlStatus.SelectedIndex == 0)
                    cmdDraftNo = txtNo.Text;

                else
                    cmdRefNo = txtNo.Text;

                int confirm;
                if (cbConfirm.Checked == true)
                    confirm = 1;
                else
                    confirm = 0;


                if (rdoApptypeIn.Checked == true)
                {
                    salaryCommandBLL.InsertSalaryCommand(rdoFlag.SelectedValue, cmdDraftNo, txtName.Text, cmdRefNo, 1101, int.Parse(ddlEmployeeType.SelectedValue), ddlStatus.SelectedValue, int.Parse(cmbEmployee.SelectedValue), cmbEmployee.SelectedItem.Text, txtAppPosition.Text, DateTime.Parse(mydate),confirm, "-", ssMng.EmpSeq);
                }
                else 
                {
                    salaryCommandBLL.InsertSalaryCommand(rdoFlag.SelectedValue, cmdDraftNo, txtName.Text, cmdRefNo, 1101, int.Parse(ddlEmployeeType.SelectedValue), ddlStatus.SelectedValue, null, txtAppName.Text, txtAppPosition.Text, DateTime.Parse(mydate), confirm, "-", ssMng.EmpSeq);
                }
            }
            catch(Exception ex)
            {
                
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                string cmdDraftNo = "", cmdRefNo = "";
                string mydate = txtDate.GetStringValue();  //18/03/2553
                if (mydate == "")
                {
                    main.MessageAlert(this.Page, "กรุณาระบุวันที่ลง!!");
                    return;
                }
                mydate = Utils.Date.ToEngFormatDate(mydate);

                if (ddlStatus.SelectedIndex == 0)
                    cmdDraftNo = txtNo.Text;

                else
                    cmdRefNo = txtNo.Text;
                int confirm;
                if (cbConfirm.Checked == true)
                    confirm = 1;
                else
                    confirm = 0;

                if (rdoApptypeIn.Checked == true)
                {
                    salaryCommandBLL.UpdateSalaryCommand(int.Parse(lblID.Text), rdoFlag.SelectedValue, cmdDraftNo, txtName.Text, cmdRefNo, 1101, int.Parse(ddlEmployeeType.SelectedValue), ddlStatus.SelectedValue, int.Parse(cmbEmployee.SelectedValue), "", txtAppPosition.Text, DateTime.Parse(mydate), confirm, "-", ssMng.EmpSeq);
                }
                else
                {
                    salaryCommandBLL.UpdateSalaryCommand(int.Parse(lblID.Text), rdoFlag.SelectedValue, cmdDraftNo, txtName.Text, cmdRefNo, 1101, int.Parse(ddlEmployeeType.SelectedValue), ddlStatus.SelectedValue, null, txtAppName.Text, txtAppPosition.Text, DateTime.Parse(mydate), confirm, "-", ssMng.EmpSeq);
                }
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            txtAppName.Enabled = false;
            cmbEmployee.Enabled = true;
            cmbEmployee.ClearSelection();
            cmbEmployee.Text = string.Empty;
            txtAppName.Text = "-";
        }
        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            txtAppName.Enabled = true;
            cmbEmployee.Enabled = false;

            cmbEmployee.ClearSelection();
            cmbEmployee.Text = string.Empty;
            cmbEmployee.Text = "... เลือกบุคลากร ...";
        }

    }
}